package day2;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import utility.JdbcUtils;


/**
 *  1. 预编译，效率高
 *  2. 防止Sql注入
 *  3.
 * @author E440
 *
 */
public class StatementVsPrepareStament {
	public static void main(String[] args) throws SQLException,InterruptedException {
		//statementDemo();
		prepareStatmenDemo();
	}
	private static void statementDemo () throws SQLException {
		// TODO Auto-generated method stub
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			// 2.建立连接
			con = JdbcUtils.getConnection();
			// 3.创建语句
			String sql = "insert into user(name,birthday, money) values ('a','2016-6-1' ,111 ) ";
			st = con.createStatement();
			st.executeUpdate(sql);
			sql = "insert into user(name,birthday, money) values ('b','2016-6-2' ,222 ) ";
			st.executeUpdate(sql);
			sql = "insert into user(name,birthday, money) values ('c','2016-6-2' ,333 ) ";
			st.executeUpdate(sql);

		}finally{
			JdbcUtils.free(rs, st, con);
			
		}
	}
	
	private static void prepareStatmenDemo () throws SQLException {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		try {
			// 2.建立连接
			con = JdbcUtils.getConnection();
			// 3.创建语句
			String sql = "insert into user(name,birthday, money) values (?,? ,?) ";
			st = con.prepareStatement(sql);
			//1
			st.setString(1, "d");
			st.setDate(2, new Date(new java.util.Date().getTime()));
			st.setFloat(3, 400f);
			st.executeUpdate();
			
			//2
			st.setString(1, "e");
			st.setDate(2, new Date(new java.util.Date().getTime()));
			st.setFloat(3, 500f);
			st.executeUpdate();
			
			//3
			st.setString(1, "f");
			st.setDate(2, new Date(new java.util.Date().getTime()));
			st.setFloat(3, 600f);
			st.executeUpdate();

		}finally{
			JdbcUtils.free(rs, st, con);
			
		}
	}
}
